package com.example.studyapp

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.studyapp.data.jsonInteraction
import com.example.studyapp.data.myRecyclerAdapter1
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import org.json.JSONObject


//笔记的部分
class study_book : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (getSupportActionBar()!=null){//去掉标题
            getSupportActionBar()?.hide()
        }
        setContentView(R.layout.activity_study_book)
        val retbtn:Button=this.findViewById(R.id.book_top_ret)
        val title:TextView=this.findViewById(R.id.book_top_title)
        title.text="笔记"
        retbtn.setOnClickListener {
            this.finish()
        }
        //联系服务器，获取笔记数据
        //val jsonobj= JSONObject(Gson().toJson(jstruct_book_list_send(username = define_user_name)))
        val js:Jstruct=myjson1.get(json_enum_dataclass.book_list)
        val jobj=JSONObject(Gson().toJson(js))
        object : jsonInteraction(jobj){
            override fun myResposeListener(response: JSONObject) {
                init_list(response)
            }
        }

    }
    //为界面填充信息
    fun init_list(strjson: JSONObject){
        val serj:Jstruct=myjson1.tojstu(strjson.toString())
        //首先判断error是否有错误
        if(serj.error){
            Toast.makeText(this, "未能访问到数据", Toast.LENGTH_SHORT)
                .show()
            return
        }
        if(serj.server.book_list.length<=0){
            Toast.makeText(this, "数据库没有数据", Toast.LENGTH_SHORT)
                .show()
            return
        }
        val layoutManager= LinearLayoutManager(this)
        val mre: RecyclerView =this.findViewById(R.id.book_recyclerview)
        mre.layoutManager=layoutManager
        //将JSONArray转换为ArrayList
        //手动修改
        val dblist=ArrayList<String>()
        dblist.add("CSMA/CD（Carrier Sense Multiple")
        dblist.add("初级会计-初级会计实务")
        dblist.add("初级会计-经济法基础")
        dblist.add("计算机二级C语言")
        dblist.add("曼彻斯特编码")
        dblist.add("DHCP客户端")
        val mylist=ArrayList<String>()
        mylist.add("13:23 带有冲突检测的载波侦听",)
        mylist.add("14:13 基本职能:核算与监督",)
        mylist.add("15:45 法律关系的要素",)
        mylist.add("17:32【考点1】C程序",)
        mylist.add("18:52 差分曼彻斯特编码",)
        mylist.add("19:32 启动时会向网络发出一个" )



        //val dblist=jsonToList<String>(serj.server.book_list.list.toString())
        //val mylist=jsonToList<String>(serj.server.book_list.idlist.toString())
        //设置选项布局
        val adapter=object : myRecyclerAdapter1(dblist, mylist){
            //设置选项单击事件
            override fun onItemClick(holder: MyViewHolder1, position: Int) {
                thisOnItemClick(holder, position)

            }
        }
        mre.adapter=adapter
    }

    //JsonArray转换成List
    private fun <T> jsonToList(jsonList: String): List<T> {
        return Gson().fromJson(jsonList, object : TypeToken<ArrayList<T>>() {}.type)
    }
    //点击选项
    fun thisOnItemClick(
            holder: myRecyclerAdapter1.MyViewHolder1,
            index: Int
    ){
        //index记录了选中的表，因此直接使用这个，由新窗口使用
        /*val jobj=JSONObject(Gson().toJson(jstruct_book_send(index = index, username = define_user_name)))
        object : jsonInteraction(jobj){
            override fun myResposeListener(response: JSONObject) {
                open_item(response)
            }
        }*/
        define_book_list_index=index
        val intent=Intent(this, book_edit::class.java)
        startActivity(intent)
    }
    fun open_item(response: JSONObject):Boolean{
        val jspi:jstruct_book_receive= Gson().fromJson(response.toString(), jstruct_book_receive::class.java)
        if(jspi.error){
            return false
        }
        //将数据通过公共变量传递给新窗口
        define_now_book_text=jspi
        val intent=Intent(this, book_edit::class.java)
        startActivity(intent)
        return true
    }
}